﻿using System;
using System.Configuration;
using System.Windows.Forms;
using Data.Common;
using DataAccess;
using WinForm.Helper;

namespace WinForm.SystemManage
{
    public partial class DbConfig : Form
    {
        public DbConfig()
        {
            InitializeComponent();
            SetSaveButtonDisable();
        }

        /// <summary>
        /// 窗体加载
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DbConfig_Load(object sender, EventArgs e)
        {
            try
            {
                string dbaddress = ConfigurationManager.AppSettings["dbAddress"];
                string dbname = ConfigurationManager.AppSettings["dbName"];
                string username = ConfigurationManager.AppSettings["dbUserName"];
                string password = ConfigurationManager.AppSettings["dbPwd"];
                InitializeTextBoxsValue(dbaddress, dbname, username, password);
            }
            catch (Exception)
            {
                MyMessageBox.ShowMessageBoxError("配置文件不完整！请联系管理员。");
            }
        }

        /// <summary>
        /// 测试连接状态
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnTest_Click(object sender, EventArgs e)
        {
            string errorMessage = string.Empty;
            try
            {
                var db = new DbBase();
                ResultInfo<bool> result = db.TestConnection(textBoxX1.Text, textBoxX2.Text, textBoxX3.Text, textBoxX4.Text);
                errorMessage = result.Describe;
                btnSave.Enabled = result.Obj;
            }
            catch (Exception e1)
            {
                errorMessage = errorMessage + e1.Message;
            }
            if (!string.IsNullOrEmpty(errorMessage))
            {
                MyMessageBox.ShowMessageBoxInfo(errorMessage);
            }
        }

        /// <summary>
        /// 保存连接信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSave_Click(object sender, EventArgs e)
        {
            string errorMessage = string.Empty;
            try
            {
                if (string.IsNullOrEmpty(textBoxX1.Text) || string.IsNullOrEmpty(textBoxX2.Text) ||
                    string.IsNullOrEmpty(textBoxX3.Text) || string.IsNullOrEmpty(textBoxX4.Text))
                {
                    errorMessage = errorMessage + "数据库配置信息不完整";
                }
                else
                {
                    Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
                    config.AppSettings.Settings["dbAddress"].Value = textBoxX1.Text;
                    config.AppSettings.Settings["dbName"].Value = textBoxX2.Text;
                    config.AppSettings.Settings["dbUserName"].Value = textBoxX3.Text;
                    config.AppSettings.Settings["dbPwd"].Value = textBoxX4.Text;
                    if (MyMessageBox.ShowMessageBoxQuestion("是否保存数据库配置信息") == DialogResult.Yes)
                    {
                        config.Save();
                        if (MyMessageBox.ShowMessageBoxQuestion("重新启动程序以新应用配置？") == DialogResult.Yes)
                        {
                            Application.Restart();
                        }
                    }
                }
            }
            catch (Exception)
            {
                MyMessageBox.ShowMessageBoxError(errorMessage);
            }
        }

        /// <summary>
        /// 取消按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnCancel_Click(object sender, EventArgs e)
        {
            Close();
            Dispose();
        }

        private void InitializeTextBoxsValue(string dbaddress, string dbname, string username, string password)
        {
            textBoxX1.Text = dbaddress;
            textBoxX2.Text = dbname;
            textBoxX3.Text = username;
            textBoxX4.Text = password;
        }

        /// <summary>
        /// 设置保存按钮不可用
        /// </summary>
        private void SetSaveButtonDisable()
        {
            btnSave.Enabled = false;
        }
    }
}